/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.dialect.HSQLDialect;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public abstract class UserConfigBase {
  private static final Map<String, Properties> EXISTING_PROPERTIES = new HashMap<String, Properties>();
  public static final String PROPS_FILE = "xptracker.properties";
  public static final String DB_LOCATION = "org.xptracker.data.db";

  public static final String DB_TYPE = "org.xptracker.data.dbtype";
  public static final String DB_TYPE_HIBERNATE = "hibernate";
  public static final String DEFAULT_DB_TYPE = DB_TYPE_HIBERNATE;

  public static final String DEFAULT_DB_LOCATION = "{0}/xptracker/db";

  public static final String HIBERNATE_DIALECT = "org.xptracker.data.hibernate.dialect";
  public static final String DEFAULT_HIBERNATE_DIALECT = HSQLDialect.class.getName();

  public static final String HIBERNATE_DRIVER = "org.xptracker.data.hibernate.driver";
  public static final String DEFAULT_HIBERNATE_DRIVER = org.hsqldb.jdbcDriver.class.getName();

  public static final String HIBERNATE_URL = "org.xptracker.data.hibernate.url";
  public static final String DEFAULT_HIBERNATE_URL_BASE = "jdbc:hsqldb:file:";
  public static final String DEFAULT_HIBERNATE_URL_SUFFIX = ";hsqldb.default_table_type=cached";
  public static final String DEFAULT_HIBERNATE_URL_FILE = "xptracker.hsqldb";

  public static final String HIBERNATE_USERNAME = "org.xptracker.data.hibernate.username";
  public static final String DEFAULT_HIBERNATE_USERNAME = "sa";

  public static final String HIBERNATE_PASSWORD = "org.xptracker.data.hibernate.password";
  public static final String DEFAULT_HIBERNATE_PASSWORD = "";

  public static final String SEARCH_LOCATION = "org.xptracker.data.search";
  public static final String DEFAULT_SEARCH_LOCATION = "{0}/xptracker/search";
  public static final String TIMEOUT = "org.xptracker.session.timeout";
  public static final String DEFAULT_TIMEOUT = "3600";
  public static final String AUTH_TYPE = "org.xptracker.auth.type";
  public static final String LDAP_HOST = "org.xptracker.auth.ldap.host";
  public static final String LDAP_BASEDN = "org.xptracker.auth.ldap.basedn";

  protected String resource;
  protected String property;
  protected String defaultValue;

  public UserConfigBase(String resource, String property, String defaultValue) {
    this.property = property;
    this.resource = resource;
    this.defaultValue = defaultValue;
  }

  public String getValue() {
    Properties properties = EXISTING_PROPERTIES.get(resource);
    final Log logger = LogFactory.getLog(getClass());
    if (properties == null) {
      properties = new Properties();
      InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
      if (is != null) {
        try {
          properties.load(is);
          EXISTING_PROPERTIES.put(resource, properties);
        } catch (IOException e) {
          logger.warn("Unable to load resource " + resource, e);
        }
      }
    }

    String value = properties.getProperty(property, defaultValue);
    String envVar = System.getProperty("os.name").startsWith("Windows") ? "USERPROFILE" : "HOME";
    final String returnValue = value == null ? null : MessageFormat.format(value, System.getenv(envVar));
    if (logger.isDebugEnabled()) {
      logger.debug(property + " = " + returnValue);
    }
    return returnValue;
  }
}
